// 必须要有 Page({}) 创建页面
Page({
  data: {
    age: 19,
    students: [],
    username: ''
  },
  onShow() {
    // 其次执行
    console.log('首页处于显示状态...')
  },

  onReady() {
    console.log('首页初次渲染完成了...')
  },

  onLoad() {
    // 最先执行
    console.log('首页面加载完毕...')
    this.getStudents()
    // console.log(this.data.students)
  },

  onHide() {
    console.log('首页处于隐藏状态...')
  },

  onUnload() {
    console.log('首页面即将卸载...')
  },
  getStudents() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    wx.request({
      url: 'https://mock.boxuegu.com/mock/3293/students',
      method: 'GET',
      success: (res) => {
        this.setData({
          students: res.data.result
        })
      },
      complete() {
        wx.hideLoading()
      }
    })
  },
  submitForm() {
    const username = this.data.username.trim()
    const reg = /^[\u4E00-\u9FA5]{2,5}$/

    if (!reg.test(username)) {
      wx.showToast({
        title: '只能是 2~5 的中文',
        icon: 'none'
      })
    }
  },
  save() {
    wx.setStorageSync('info', {
      name: 'zs',
      address: '河南'
    })
    wx.setStorageSync('name', '张飞')
  },
  get() {
    const info = wx.getStorageSync('info')
    console.log(info)
  },
  removeOne() {
    wx.removeStorageSync('name')
  },
  removeAll() {
    wx.clearStorageSync()
  }
})